/**
 * @param {number} n
 * @return {string[]}
 */

var generateParenthesis = function (n) {
    let res = [];
    if (n <= 0) {
        return res
    }
    getParenthesis(res, '', n, n);
    return res;
};

let getParenthesis = function (res, str, left, right) {
    if (left === 0 && right === 0) {
        res.push(str);
        return
    };
    if (left === right) {
        getParenthesis(res, str + '(', left - 1, right);
    }
    if (left < right) {
        if (left > 0) {
            getParenthesis(res, str + '(', left - 1, right);
        }
        getParenthesis(res, str + ')', left, right - 1);
    }
}